package com.x.provider.general.service.cms;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.x.provider.general.mapper.CategoryMapper;
import com.x.provider.general.model.domain.cms.Category;
import com.x.provider.general.model.query.cms.CategoryQuery;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author: liushenyi
 * @date: 2022/10/31/11:00
 */
@Service
public class CategoryServiceImpl implements CategoryService{

    private final CategoryMapper categoryMapper;

    public CategoryServiceImpl(CategoryMapper categoryMapper){
        this.categoryMapper = categoryMapper;
    }

    @Override
    public List<Category> listCategory(Integer categoryType) {
        return listCategory(CategoryQuery.builder().categoryType(categoryType).build());
    }

    private List<Category> listCategory(CategoryQuery categoryQuery) {
        final LambdaQueryWrapper<Category> queryWrapper = build(categoryQuery);
        return categoryMapper.selectList(queryWrapper);
    }

    private Category getCategory(CategoryQuery categoryQuery) {
        final LambdaQueryWrapper<Category> queryWrapper = build(categoryQuery);
        return categoryMapper.selectOne(queryWrapper);
    }



    private LambdaQueryWrapper<Category> build(CategoryQuery categoryQuery) {
        final LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        if (categoryQuery.getCategoryType() != null){
            queryWrapper.eq(Category::getCategoryType, categoryQuery.getCategoryType());
        }
        queryWrapper.orderByAsc(Category::getDisplayOrder);
        return queryWrapper;
    }


}
